class Watch {
  constructor(vue, exp, cb) {
    this.vue = vue
    this.exp = exp
    this.cb = cb
    // 将当前实例放到Watcher中，放在这里是为了防止update调用get时，重复添加依赖
    Watcher = this
    // 得到当前vue实例上对应表达式exp的最新值
    this.value = this.get()
    // 将Watcher置空，准备接受下一个值
    Watcher = null
  }
  get() {
    var exps = this.exp.split('.')
    var obj = this.vue
    for (var i = 0, len = exps.length; i < len; i++) {
      if (!obj) return
      obj = obj[exps[i]]
    }
    var value = obj
    return value
  }
  update() {
    let value = this.get()
    let oldVal = this.value
    if (value !== oldVal) {
      this.value = value
      // 将此变量相关的回调函数执行
      this.cb.call(this.vue, value)
    }
  }
}